package com.shenqi.parking.service;

import com.shenqi.parking.utils.OSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PreDestroy;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

@Service
public class FileUploadService {

    private OSSUtil ossUtil;

    public FileUploadService(
            @Value("${aliyun.oss.endpoint}") String endpoint,
            @Value("${aliyun.oss.access-key-id}") String accessKeyId,
            @Value("${aliyun.oss.access-key-secret}") String accessKeySecret,
            @Value("${aliyun.oss.bucket-name}") String bucketName) {
        this.ossUtil = new OSSUtil(endpoint, accessKeyId, accessKeySecret, bucketName);
    }

    public String uploadImage(InputStream fileInputStream, String filename, String uploadPath) throws IOException {
        return ossUtil.uploadImage(fileInputStream, filename, uploadPath);
    }


    @PreDestroy
    public void shutdownOSSClient() {
        if (ossUtil != null) {
            ossUtil.shutdown();
        }
    }
}